﻿Imports System.Collections.Generic
Imports System.Linq
Partial Class Subscription1
    Inherits System.Web.UI.Page
    Dim SubID As Integer
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        SubID = Request("subid")

        Dim SQLDB As New OneCall.DataModel.OneCDataEntities

        Dim SubData As OneCall.DataModel.Subscription = (From p In SQLDB.Subscriptions Where p.Subscription_ID = SubID).ToList()(0)

        Dim EndDate As DateTime = Now().AddDays(SubData.Subscription_Duration)


        lblSubType.Text = SubData.Subscription_Name
        lblSubStartDate.Text = Now()
        lblSubEndDate.Text = EndDate
        lblSubAmount.Text = "Rs." & FormatNumber(SubData.Subscription_Amount, 2)

        CreateCaptcha()

    End Sub
    Sub CreateCaptcha()
        Dim iniTime As String = Now().TimeOfDay.ToString()
        txtIniTime.Value = iniTime
        Dim regkey As String = OneCall.Crypt.Encrypt(iniTime, "1CHCARENEWSUBSC")
        regkey = Left(regkey, 6)
        imgCap.ImageUrl = "capimg.ashx?str=" & regkey
    End Sub

    Protected Sub btnSubscribe_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubscribe.Click
        Dim str As String = txtIniTime.Value
        str = OneCall.Crypt.Encrypt(str, "1CHCARENEWSUBSC")
        str = Left(str, 6)
        If (str = txtImageText.Text) Then
            lblErrMsg.Text = ""
            Dim SQLDB As New OneCall.DataModel.OneCDataEntities()
            Dim cUser As New OneCall.User.AuthUser(Request, Response)
            Dim SubID As Integer = Request("subid")
            Dim NewMemberShip As New OneCall.DataModel.Membership()
            NewMemberShip.Membership_Customer_ID = Int(cUser.GetUserID())
            NewMemberShip.Membership_StartDate = CDate(lblSubStartDate.Text)
            NewMemberShip.Membership_EndDate = CDate(lblSubEndDate.Text)
            NewMemberShip.Membership_Subscription_ID = Int(SubID)
            NewMemberShip.Membership_Status = 0
            SQLDB.Memberships.AddObject(NewMemberShip)
            SQLDB.SaveChanges()

            panelForm.Visible = False
            panelMessage.Visible = True
            Dim custid As Integer = cUser.GetUserID()
            Dim customers As OneCall.DataModel.Customer = (From p In SQLDB.Customers Where p.Customer_ID = custid).ToArray()(0)
            Dim msg As String = ""
            msg = msg & "Hi " & cUser.GetUserFullName() & "<br>"
            msg = msg & "<br>"
            msg = msg & "Thank you for your subscribing to our service." & "<br>" & "<br>"
            msg = msg & "The following are the subscription details" & "<br>"
            msg = msg & "==============================================" & "<br>"
            msg = msg & "Subscription service: " & lblSubType.Text & "<br> "
            msg = msg & "Subscription start date: " & lblSubStartDate.Text & "<br> "
            msg = msg & "Subscription end date: " & lblSubEndDate.Text & "<br> "
            msg = msg & "Subscription amount: " & lblSubAmount.Text & "<br> "
            msg = msg & "==============================================" & "<br>"
            msg = msg & "<br>"
            msg = msg & "Regards," & "<br>"
            msg = msg & "Customer service" & "<br>"
            msg = msg & "One Call Home Care Solution" & "<br>"
            msg = msg & "www.onecallhomecaresolution.com" & "<br>"

            OneCall.EmailGateway.EMailSender.SendWebEmail(cUser.GetEmailID(), "Subscription email", msg)
            OneCall.SMSGateway.SMS.SendSMS(cUser.GetUserFullName() & ", OneCallhomeCareSolution welcomes you, your subscription is under process, our executive will get in touch with you shortly", customers.Customer_Mobile)

            SQLDB.Dispose()
        Else
            lblErrMsg.Text = "Invalid text"
            CreateCaptcha()
        End If

    End Sub
End Class
